package cn.itcast.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class loginInterceptor implements HandlerInterceptor {

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(request.getRequestURI().contains("register")){
            return true;
        }
        if(request.getRequestURI().contains("login")){
            return true;
        }
        if(request.getRequestURI().contains("turnToRegister")){
            return true;
        }
        if(request.getRequestURI().contains("turnToLogin")){
            return true;
        }
        if(redisTemplate.opsForHash().hasKey("token","user")){
            return true;
        }
        request.getRequestDispatcher("WEB-INF/pages/login.jsp").forward(request,response);
        return false;
    }
}
